/**
 * Created by xuechao on 2016/9/2.
 */
app.controller("trafficCtrl", function ($scope, trafficService) {

    /**
     * 时间格式化
     * @param fmt
     * @returns {*}
     * @constructor
     */
    Date.prototype.Format = function (fmt) {
        var o = {
            "M+": this.getMonth() + 1,               //月份
            "d+": this.getDate(),                    //日
            "h+": this.getHours(),                   //小时
            "m+": this.getMinutes(),                 //分
            "s+": this.getSeconds()                  //秒
        };
        if (/(y+)/.test(fmt))
            fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }

    var searchRawMsg = '';
    var searchMsgResult = '0000';
    var searchDateStart = (new Date()).Format("yyyy-MM-dd") + " 00:00";
    var searchDateEnd = (new Date()).Format("yyyy-MM-dd") + " 23:59";
    var searchSend = '';
    var searchReceive = '';
    var searchType = '';

    //页面时间输入框初始化
    $scope.searchDateStart = (new Date()).Format("yyyy-MM-dd") + " 00:00";
    $scope.searchDateEnd = (new Date()).Format("yyyy-MM-dd") + " 23:59";

    /**
     * 页面初始化数据加载
     */
    trafficService.findByConditions({
        action: 'getTrafficsByCondition',
        dateStart: searchDateStart,
        dateEnd: searchDateEnd,
        error_code : searchMsgResult,
        currentPage: 1,
        pageSize: 25
    }, function (data) {
        if (data.state == 'SUCCESS') {
            $scope.traffics = data.pageInfo.list;
            pageOperation($scope, data.pageInfo);
        } else {
            message_alert('提示',"查询失败(" + data.reason + ")",null);
        }
    });

    /**
     * 发送方、接收方
     */
    trafficService.getUsersByCondition({
        action: 'getUsersByCondition'
    }, function (data) {
        //展示数据用户
        if (data.state == 'SUCCESS') {
            $scope.trafficUsers = data.data.GWUserVO;
        } else {
            message_alert('提示',"查询失败(" + data.reason + ")",null);
        }
    });
    /**
     * 查询按钮事件
     */
    $scope.findByConditions = function () {
        searchRawMsg = $scope.searchRawMsg;
        searchMsgResult = $scope.searchMsgResult;
        searchDateStart = $scope.searchDateStart;
        searchDateEnd = $scope.searchDateEnd;
        searchSend = $scope.searchSend;
        searchReceive = $scope.searchReceive;
        searchType = $scope.searchType;
        $scope.pageSize = $scope.selectPageSize
        trafficService.findByConditions({
            action: 'getTrafficsByCondition',
            currentPage: 1,
            pageSize: $scope.pageSize,
            rawMsg: searchRawMsg,
            error_code : searchMsgResult,
            dateStart: searchDateStart,
            dateEnd: searchDateEnd,
            sender: searchSend,
            receiver: searchReceive,
            msgType: searchType
        }, function (data) {
            if (data.state == 'SUCCESS') {
                $scope.traffics = data.data.TrafficVO;
                pageOperation($scope, data.pageInfo);
            } else {
                message_alert('提示',"查询失败(" + data.reason + ")",null);
            }
        });
    }

    /**
     * 分页查询事件
     */
    $scope.findByCondition = function () {
        trafficService.findByConditions({
            action: 'getTrafficsByCondition',
            currentPage: $scope.selPage,
            pageSize: $scope.pageSize,
            rawMsg: searchRawMsg,
            error_code : searchMsgResult,
            dateStart: searchDateStart,
            dateEnd: searchDateEnd,
            sender: searchSend,
            receiver: searchReceive,
            msgType: searchType
        }, function (data) {
            if (data.state == 'SUCCESS') {
                $scope.traffics = data.data.TrafficVO;
                pageOperation($scope, data.pageInfo);
            } else {
                message_alert('提示',"查询失败(" + data.reason + ")",null);
            }
        });
    }
    //时间空间属性设置
    $('#dateStart').datetimepicker({
        language: 'zh-CN',
        todayBtn: 1,
        autoclose: 1,
        endDate: new Date()
    }).on("changeDate", function () {
        $("#dateEnd").datetimepicker("setStartDate", $("#dateStart").val());
    });
    $('#dateEnd').datetimepicker({
        language: 'zh-CN',
        todayBtn: 1,
        autoclose: 1,
        endDate: new Date()
    }).on("changeDate", function () {
        $("#dateStart").datetimepicker("setEndDate", $("#dateEnd").val());
    });
    $scope.export = function () {
        if ($scope.total <= 10000) {
            if ($scope.total == null || $scope.total == 0) {
                message_alert('提示','数据为空，请根据查询条件重新查询数据！',null);
            } else {
                var url = "traffic/exportExcel?rawMsg=" + searchRawMsg + "&dateStart=" + searchDateStart + "&dateEnd=" + searchDateEnd
                    + "&sender" + searchSend + "receiver" + searchReceive + "msgType" + searchType;
                window.open(url)
            }
        } else {
            message_alert('提示','导出数据量过大,超过10000条记录，请缩小符合条件范围！',null);
        }
    }
});


